package com.swt.controller.S_controller;

import com.alibaba.fastjson.JSON;
import com.swt.domain.Course;
import com.swt.service.S_service.GetCourseService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class GetCourseController {
    /*注入service*/
    @Resource(name = "S_getCourseService")
    private GetCourseService getCourseService;
    /*根据店铺获取该店铺下面的所有的团课信息*/
    @RequestMapping("/S_getCourse")
    @ResponseBody
    public String getCoutse(int clubid){
        /*调用service层*/
        /*模拟店铺id*/
        List<Course> list = getCourseService.getCourse(clubid);
        String courselist=JSON.toJSONString(list);
        return courselist;
    }
    /*获取该店铺下面的所有的私教信息*/
    @RequestMapping("/S_getCoursePersonal")
    @ResponseBody
    public String getCoutsePersonal(int clubid){
        /*调用service层*/
        /*模拟店铺id*/
        List<Course> list = getCourseService.getCoursePersonal(clubid);
        String courselist=JSON.toJSONString(list);
        return courselist;
    }
    /*根据课程ID查询对应课程*/
    @RequestMapping("/S_getCourseById")
    @ResponseBody
    public  String getCourseById(int courseId){
        /*调用service层*/
        Course course = getCourseService.getCourseById(courseId);
        String course1=JSON.toJSONString(course);
        return course1;
    }
    /*根据条件查询课程*/
    @RequestMapping("/S_getCourseBySelect")
    @ResponseBody
    public String getCourseBySelect(int club,String date,String time,String difficult){
        System.out.println("club="+club);
        System.out.println("date="+date);
        System.out.println("time="+time);
        System.out.println("difficult="+difficult);
        /*生成多条件查询*/
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Course.class);
        /*设置类型为团课 因为这是预约团课的*/
        detachedCriteria.add(Restrictions.eq("courseCatagory","团课"));
        //1.添加店铺id条件
        detachedCriteria.add(Restrictions.eq("courseClubid",club));
        //2.添加日期条件
        if(!"".equals(date)){
            detachedCriteria.add(Restrictions.eq("courseDate",date));
        }
        //3.添加时间条件
        if(!"请选择时间".equals(time)){
            detachedCriteria.add(Restrictions.eq("courseTime",time));
        }
        //4.添加难度条件
        if(!"请选择难度".equals(difficult)){
            detachedCriteria.add(Restrictions.eq("courseDifficulty",difficult));
        }
        /*调用service*/
        List<Course> list = getCourseService.getCourseBySelect(detachedCriteria);
        String course=JSON.toJSONString(list);
        return course;
    }
    /*根据条件查询课程*/
    @RequestMapping("/S_getCourseByPersonal")
    @ResponseBody
    public String getCourseByPersonal(int club,String date,String time,String difficult){
        System.out.println("club="+club);
        System.out.println("date="+date);
        System.out.println("time="+time);
        System.out.println("difficult="+difficult);
        /*生成多条件查询*/
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Course.class);
        /*设置类型为团课 因为这是预约团课的*/
        detachedCriteria.add(Restrictions.eq("courseCatagory","私教"));
        //1.添加店铺id条件
        detachedCriteria.add(Restrictions.eq("courseClubid",club));
        //2.添加日期条件
        if(!"".equals(date)){
            detachedCriteria.add(Restrictions.eq("courseDate",date));
        }
        //3.添加时间条件
        if(!"请选择时间".equals(time)){
            detachedCriteria.add(Restrictions.eq("courseTime",time));
        }
        //4.添加难度条件
        if(!"请选择难度".equals(difficult)){
            detachedCriteria.add(Restrictions.eq("courseDifficulty",difficult));
        }
        /*调用service*/
        List<Course> list = getCourseService.getCourseBySelect(detachedCriteria);
        String course=JSON.toJSONString(list);
        return course;
    }
}
